package cn.iocoder.yudao.module.trade.order.vo;

import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.framework.thirdparty.zxhx.dto.ZXHXOrderSubmitRespDTO;
import cn.iocoder.yudao.framework.validation.InEnumValue;
import cn.iocoder.yudao.module.api.dine.enums.DineStatusEnum;
import cn.iocoder.yudao.module.api.member.user.dto.MemberUserSimpleRespDTO;
import cn.iocoder.yudao.module.api.product.sku.dto.ProductPropertyValueRespDTO;
import cn.iocoder.yudao.module.api.promotion.combination.dto.CombinationRecordRespDTO;
import cn.iocoder.yudao.module.api.promotion.groupon.dto.GrouponPackageRespDTO;
import cn.iocoder.yudao.module.api.shop.staff.vo.ShopStaffVO;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Schema(description = "管理后台 - 交易订单的详情 Response VO")
@Data
public class TradeOrderDetailRespVO extends TradeOrderBaseVO {

    @Schema(description = "订单状态名称", example = "待付款")
    private String statusName;

    @Schema(description = "订单项列表")
    private List<Item> items;

    @Schema(description = "下单用户信息")
    private MemberUserSimpleRespDTO user;

    @Schema(description = "推广用户信息")
    private MemberUserSimpleRespDTO brokerageUser;

    @Schema(description = "操作日志列表")
    private List<OrderLog> logs;

    @Schema(description = "点餐状态：0-无、10-待接单、20-备餐中、21-已拒单、22-已派单、30-已出餐、40-配送中、50-已送达", example = "1")
    @InEnumValue(DineStatusEnum.class)
    private Integer dineStatus;
    private String dineStatusName;

    @SuppressWarnings("unused")
    public String getDineStatusName() {
        if (dineStatusName == null) {
            return DineStatusEnum.getByValue(getDineStatus()).getName();
        }
        return dineStatusName;
    }

    @Schema(description = "接单时间，点餐下单时提供")
    private LocalDateTime acceptedTime;

    @Schema(description = "派单时间，点餐下单时提供")
    private LocalDateTime assignedTime;

    @Schema(description = "出餐时间，点餐下单时提供")
    private LocalDateTime packedTime;

    @Schema(description = "配送人员编号，外卖派单时提供", example = "1")
    private Long riderStaffId;

    @Schema(description = "配送人员编号，外卖派单时提供")
    private ShopStaffVO riderStaff;

    @Schema(description = "配送人员提交的凭证图片")
    private List<String> riderPicUrls;

    @Schema(description = "配送人员提交的备注")
    private String riderRemark;

    @Schema(description = "收件人地区名字", requiredMode = Schema.RequiredMode.REQUIRED, example = "上海 上海市 普陀区")
    private String receiverAreaName;

    @Schema(description = "拼团记录编号，拼团订单才有值", example = "100")
    private Long combinationRecordId;

    @Schema(description = "拼团记录，拼团订单才有值")
    private CombinationRecordRespDTO combinationRecord;

    // ========== 团购活动相关字段 ==========

    @Schema(description = "团购套餐编号，团购订单才有值")
    private Long grouponPackageId;

    @Schema(description = "团购套餐，团购订单才有值")
    private GrouponPackageRespDTO grouponPackage;

    // ========== 堂食点餐相关字段 ==========

    @Schema(description = "点餐餐桌编号，点餐下单时提供", example = "0")
    private Long dineDeskId;

    @Schema(description = "用餐人数，点餐下单时提供", example = "0")
    private Integer dinePeoples;

    @Schema(description = "预定日期，预约下单才有值")
    private LocalDate reserveDate;

    @Schema(description = "预定时间段，如12:00-12:30，预约下单才有值")
    private String reservePeriod;

    @Schema(description = "管理后台 - 交易订单的操作日志")
    @Data
    public static class OrderLog {

        @Schema(description = "操作详情", requiredMode = Schema.RequiredMode.REQUIRED, example = "订单发货")
        private String content;

        @Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED, example = "2023-06-01 10:50:20")
        private LocalDateTime createTime;

        @Schema(description = "用户类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
        private Integer userType;

    }

    @Schema(description = "管理后台 - 交易订单的详情的订单项目")
    @Data
    public static class Item extends TradeOrderItemBaseVO {

        /**
         * 属性数组
         */
        private List<ProductPropertyValueRespDTO> properties;

    }

    @Schema(description = "供应方名称")
    private String supplyName;

    @Schema(description = "供应方关联单号")
    private List<String> supplyRelatedNos;

    @Schema(description = "供应方关联订单")
    @JsonIgnore
    private String supplyRelatedOrders;

    @Schema(description = "供应方关联订单")
    private List<ZXHXOrderSubmitRespDTO> supplyRelatedOrderList;

    @SuppressWarnings("unused")
    public List<ZXHXOrderSubmitRespDTO> getSupplyRelatedOrderList() {
        if (!JsonUtils.isJson(supplyRelatedOrders)) {
            return null;
        }
        return JsonUtils.parseArray(supplyRelatedOrders, ZXHXOrderSubmitRespDTO.class);
    }

}
